package com.example.bbs.controller;

import com.example.bbs.mapper.MessageMapper;
import com.example.bbs.pojo.Message;
import com.example.bbs.pojo.PageBean;
import com.example.bbs.pojo.Result;
import com.example.bbs.service.MessageService;
import com.example.bbs.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    public MessageMapper messageMapper;

    @Autowired
    public MessageService messageService;
    //添加留言
    @PostMapping("/addMessage")
    public Result addMessage(@RequestBody Message message){
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        message.setUsername(username);
        messageMapper.addMessage(message);
        return Result.success();
    }

    //分页查询留言
    @GetMapping
    public Result<PageBean<Message>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Message> pb =  messageService.list(pageNum,pageSize);
        return Result.success(pb);
    }

    //根据账号查询留言
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(messageMapper.getById(id));
    }

    //删除留言
    @PostMapping("/delete")
    public Result deleteMessage(Integer messageId){
        messageMapper.deleteMessage(messageId);
        return Result.success();
    }
//    //查询所有留言
//    @GetMapping("/messages")
//    public Result getMessages(){
//        System.out.println(messageMapper.getMessages());
//        return Result.success(messageMapper.getMessages());
//    }


}
